import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;


public class DialogConnexion extends JDialog {
	private JTextField textFieldPseudo;
	private JTextField textFieldServeur;
	private Client controleur;
	private JPasswordField passwordField;
	
	public DialogConnexion(Client controleur) {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				fermerFenetre();
			}
		});
		this.controleur = controleur;
		
		this.setBounds(50,50,455,191);	
		setResizable(false);
		setTitle("Connexion au Tchat");
		getContentPane().setLayout(null);
		
		textFieldPseudo = new JTextField();
		textFieldPseudo.setBounds(169, 12, 261, 19);
		getContentPane().add(textFieldPseudo);
		textFieldPseudo.setColumns(10);
		
		JLabel lblPseudo = new JLabel("Pseudo");
		lblPseudo.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPseudo.setBounds(12, 14, 139, 15);
		getContentPane().add(lblPseudo);
		
		JLabel lblMotDePasse = new JLabel("Mot de passe");
		lblMotDePasse.setHorizontalAlignment(SwingConstants.RIGHT);
		lblMotDePasse.setBounds(12, 45, 139, 15);
		getContentPane().add(lblMotDePasse);
		
		JLabel lblServeur = new JLabel("Serveur");
		lblServeur.setHorizontalAlignment(SwingConstants.RIGHT);
		lblServeur.setBounds(12, 76, 139, 15);
		getContentPane().add(lblServeur);
		
		textFieldServeur = new JTextField();
		textFieldServeur.setColumns(10);
		textFieldServeur.setBounds(169, 74, 261, 19);
		getContentPane().add(textFieldServeur);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(169, 43, 261, 19);
		getContentPane().add(passwordField);
		
		JButton btnConnexion = new JButton("Connexion");
		btnConnexion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				connexion();
			}
		});
		btnConnexion.setBounds(328, 126, 107, 25);
		getContentPane().add(btnConnexion);
		
		JButton btnCreerCompte = new JButton("Créer un compte");
		btnCreerCompte.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ouvrirDialogueCreerCompte();
			}
		});
		btnCreerCompte.setBounds(177, 126, 139, 25);
		getContentPane().add(btnCreerCompte);
		
		JButton btnFermer = new JButton("Fermer");
		btnFermer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fermerFenetre();
			}
		});
		btnFermer.setBounds(58, 126, 107, 25);
		getContentPane().add(btnFermer);
	}
	
	public void fermerFenetre()
	{
		this.controleur.fermerDialogueConnexion();
	}
	
	public void connexion()
	{
		String pseudo = this.textFieldPseudo.getText();
		String password = new String(this.passwordField.getPassword());
		String hote = this.textFieldServeur.getText();
		if(this.controleur.ouvrirConnexion(2000, hote, pseudo, password))
		{
			this.controleur.ouvrirFenetreChat();
			fermerFenetre();
		}
		else
		{
			JOptionPane.showMessageDialog(this, "Erreur : connexion impossible", "Erreur", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public void ouvrirDialogueCreerCompte()
	{
		this.controleur.ouvrirDialogCreerCompte();
		fermerFenetre();
	}
}
